package d_javamail;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class JavaMail {

	private static Properties props;
	private static String host;
	private static String username;
	private static String password;

	static {
		init();
	}

	public static void init() {
		props = new Properties();
		InputStream in = JavaMail.class.getClassLoader().getResourceAsStream(
				"mail.properties");
		try {
			props.load(in);
			host = props.getProperty("host");
			username = props.getProperty("username");
			password = props.getProperty("password");
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 简单发送邮件
	 */
	public void sendSimpleMailMessage() {
		SimpleMailMessage message = new SimpleMailMessage();
		// 设置收件人
		message.setTo("277648798@qq.com");
		/*
		 * //可以实现邮件群发 String[]
		 * msgTo={"277648798@qq.com","carryman1990@163.com"};
		 * message.setTo(msgTo);
		 */
		// 设置发件人
		message.setFrom("carryman1990@163.com");
		// 设置标题
		message.setSubject("测试");
		// 设置内容
		message.setText("要感冒呀,o(╯□╰)o");

		// 创建javaMailSender实现类
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		// //创建配置文件
		// Properties prop = new Properties();
		// 配置文件,实现服务器的验证(很重要)
		// prop.put("mail.smtp.auth", "true");
		sender.setHost(host);
		sender.setUsername(username);
		sender.setPassword(password);
		sender.setJavaMailProperties(props);
		sender.send(message);

	}

	/*
	 * 2.实现邮件发送之HTML邮件
	 */
	public void sendHtmlMail() {
		// 创建sender实现类,配置发件人信息
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost(host);
		sender.setUsername(username);
		sender.setPassword(password);
		sender.setJavaMailProperties(props);
		// 创建MimeMessageHelper
		MimeMessage message = sender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(message);
		// 设置发件信心
		try {
			messageHelper.setTo("277648798@qq.com");
			messageHelper.setFrom("carryman1990@163.com");
			messageHelper.setSubject("HTML邮件发送");
			messageHelper
					.setText(
							"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body><h1>这是用实现HTML邮件发送的测试,upup</h1></body></html>",
							true);
			sender.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}
	
	/*
	 * 3.实现邮件发送之内嵌图片+附件
	 */
	public void sendImageMail(){
		// 创建sender实现类,配置发件人信息
				JavaMailSenderImpl sender = new JavaMailSenderImpl();
				sender.setHost(host);
				sender.setUsername(username);
				sender.setPassword(password);
				sender.setJavaMailProperties(props);
				
				// 创建MimeMessageHelper
				MimeMessage message = sender.createMimeMessage();
				 //  在构建MimeMessageHelper时候，所给定的值是true表示启用，     
		         //multipart模式
				try {
					MimeMessageHelper messageHelper = new MimeMessageHelper(message,true,"utf-8");
					messageHelper.setTo("277648798@qq.com");
					messageHelper.setFrom("carryman1990@163.com");
					messageHelper.setSubject("HTML嵌入图片测试");
					messageHelper
							.setText(
									"<h1>这是用实现HTML邮件发送的测试,upup</h1>"
									+ "<img src=\"cid:image\" title=\"嵌入图片\" alt=\"嵌入图片\"/>"
									+"<h1>图片夹在中间</h1>"
									,
									true);
					//加载图片,增加CID内容 
					ClassPathResource img = new ClassPathResource("leave.png");
					messageHelper.addInline("image", img);
					//添加附件
					FileSystemResource fujian = new FileSystemResource(new File("D://123.jpg"));
					messageHelper.addAttachment("123.jpg", fujian);
					//发送邮件
					sender.send(message);
					
				} catch (MessagingException e) {
					e.printStackTrace();
				}
				
	}
	

	public static void main(String[] args) {
		JavaMail javaMail = new JavaMail();
		// 1.实现简单邮件发送
//		javaMail.sendSimpleMailMessage();
		//2.实现邮件发送之HTML邮件
//		javaMail.sendHtmlMail();
		//3.实现邮件发送之内嵌图片+附件
		javaMail.sendImageMail();

	}

}
